package com.rnkrsoft.suyan.config.server.services.impl;

import com.rnkrsoft.suyan.config.internal.enums.OperateObjectEnum;
import com.rnkrsoft.suyan.config.internal.enums.OperateTypeEnum;
import com.rnkrsoft.suyan.config.server.config.dao.*;
import com.rnkrsoft.suyan.config.server.config.entity.OperateHistoryEntity;
import com.rnkrsoft.suyan.config.server.services.logger.services.LoggerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by woate on 2019/7/27.
 */
@Service
public class LoggerServiceImpl implements LoggerService {
    @Autowired
    ProjectInfoDAO projectInfoDAO;
    @Autowired
    VersionInfoDAO versionInfoDAO;
    @Autowired
    EnvironmentInfoDAO environmentInfoDAO;
    @Autowired
    MachineInfoDAO machineInfoDAO;
    @Autowired
    ParamInfoDAO paramInfoDAO;
    @Autowired
    FileInfoDAO fileInfoDAO;
    @Autowired
    OperateHistoryDAO operateHistoryDAO;
    @Autowired
    OperateHistoryProjectDAO operateHistoryProjectDAO;
    @Autowired
    OperateHistoryVersionDAO operateHistoryVersionDAO;
    @Autowired
    OperateHistoryEnvironmentDAO operateHistoryEnvironmentDAO;
    @Autowired
    OperateHistoryMachineDAO operateHistoryMachineDAO;
    @Autowired
    OperateHistoryParamDAO operateHistoryParamDAO;
    @Autowired
    OperateHistoryFileDAO operateHistoryFileDAO;

    @Override
    public void writeLog(OperateObjectEnum operateObject, String primaryId, OperateTypeEnum operateType, Integer operatorId, String operatorName) {
        OperateHistoryEntity operateHistoryEntity = new OperateHistoryEntity();
        operateHistoryEntity.setOperatorId(operatorId);
        operateHistoryEntity.setOperatorName(operatorName);
        operateHistoryDAO.insertSelective(operateHistoryEntity);
        String operateId = operateHistoryEntity.getOperateId();
        if (operateObject == OperateObjectEnum.PROJECT) {
            operateHistoryProjectDAO.createSnapshot(operateId, primaryId);
            operateHistoryVersionDAO.createSnapshot(operateId, primaryId, null);
            operateHistoryEnvironmentDAO.createSnapshot(operateId, primaryId, null, null);
            operateHistoryMachineDAO.createSnapshot(operateId, primaryId, null, null, null);
            operateHistoryParamDAO.createSnapshot(operateId, primaryId, null, null, null, null);
        } else if (operateObject == OperateObjectEnum.VERSION) {
            operateHistoryVersionDAO.createSnapshot(operateId, null, primaryId);
            operateHistoryEnvironmentDAO.createSnapshot(operateId, null, primaryId, null);
            operateHistoryMachineDAO.createSnapshot(operateId, null, primaryId, null, null);
            operateHistoryParamDAO.createSnapshot(operateId, null, primaryId, null, null, null);
        } else if (operateObject == OperateObjectEnum.ENVIRONMENT) {
            operateHistoryEnvironmentDAO.createSnapshot(operateId, null, null, primaryId);
            operateHistoryMachineDAO.createSnapshot(operateId, null, null, primaryId, null);
            operateHistoryParamDAO.createSnapshot(operateId, null, null, primaryId, null, null);
        } else if (operateObject == OperateObjectEnum.MACHINE) {
            operateHistoryMachineDAO.createSnapshot(operateId, null, null, null, primaryId);
            operateHistoryParamDAO.createSnapshot(operateId, null, null, null, primaryId, null);
        } else if (operateObject == OperateObjectEnum.PARAM) {
            operateHistoryParamDAO.createSnapshot(operateId, null, null, null, null, primaryId);
        } else if (operateObject == OperateObjectEnum.FILE) {

        }
    }
}
